package com.shop.controller;


import com.shop.interceptor.CartInterceptor;
import com.shop.service.Cart_Service;
import com.shop.utils.R;
import com.shop.vo.Cart;
import com.shop.vo.CartItem;
import com.shop.vo.UserInfoTo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.concurrent.ExecutionException;

@RestController
@RequestMapping("/cart")
public class Cart_Controller {

    @Autowired
    private Cart_Service cart_service;


    /**
     * 获取当前登录用户的购物项
     */
    @GetMapping("/getUserByIdCartItems")
    public List<CartItem>  getUserByIdCartItems(){

        return cart_service.getUserByIdCartItems();
    }




    /**
     * 测试获取session|user-key是否获取到
     * @return
     */
    @GetMapping("/test")
    public String test(){
        //先查询
        UserInfoTo userInfoTo = CartInterceptor.toThreadLocal.get();
            System.out.println(userInfoTo);
        return null;
    }

    /**
     * 添加商品到购物车
     * @param skuId
     * @param num
     * @return
     */
    @GetMapping("/addToCart")
    public R addToCart(Long skuId,Integer num) throws ExecutionException, InterruptedException {

        //首先先去查询我们是否创建过临时的用户user-key  要不然的话我们会不断的创建临时用户信息
        System.out.println(skuId+","+num);
        //返回购物项方便页面可以拿到值进行获取的展示
//       CartItem cartItem =  cart_service.addToCart(skuId,num);

        return cart_service.addToCart(skuId,num);
    }

    /**
     * 获取所有购物车
     * @return
     */
    @GetMapping("/getCarts")
    public Cart getCarts() throws ExecutionException, InterruptedException {

        return cart_service.getCarts();
    }

    /**
     * 删除购物的的商品缓存信息
     * @param skuId
     * @return
     */
    @DeleteMapping("/deleteCarts")
    public R deleteCarts(String skuId){
        System.out.println("要删除的购物车商品缓存ID:"+skuId);
        return cart_service.deleteCarts(skuId);
    }

    /***
     * 修改购物车商品的check选中状态
     * @param skuId
     * @param check
     * @return
     */
    @GetMapping("/cartUpChecked")
    public R cartUpChecked(Long skuId,Boolean check){
        System.out.println("修改的商品check状态:"+skuId+"-"+check);
        return cart_service.cartUpChecked(skuId,check);
    }





}
